package reseau;

public class Reseau 
{
	protected int nombreWaypointsMaximum;
	
	protected WayPoint[] waypoints;
	protected int nombreWayPoints=0;
	
	protected double[][] couts;
	
	public Reseau(int nombreWaypointsMaximum)
	{
		this.nombreWaypointsMaximum = nombreWaypointsMaximum;
		this.waypoints = new WayPoint[this.nombreWaypointsMaximum];
		
		this.couts = new double[this.nombreWaypointsMaximum][this.nombreWaypointsMaximum];
		for(int i=0; i<this.nombreWaypointsMaximum; ++i)
			for(int j=0; j<this.nombreWaypointsMaximum; ++j)
				this.couts[i][j] = (i==j)?0:Double.POSITIVE_INFINITY;
	}
	
	public void ajouterWayPoint(int x, int y)
	{
		if(this.nombreWayPoints<this.nombreWaypointsMaximum)
		{
			this.waypoints[this.nombreWayPoints] = new WayPoint(this.nombreWayPoints, new Coordonnees2D(x,y));
			double distance;
			for(int i=0; i<this.nombreWayPoints; ++i)
			{
				distance = this.calculDistance(this.waypoints[i], this.waypoints[this.nombreWayPoints]);
				this.couts[this.nombreWayPoints][i] = distance;
				this.couts[i][this.nombreWayPoints] = distance;
			}
			++this.nombreWayPoints;
		}
	}
	
	protected double calculDistance(WayPoint w1, WayPoint w2)
	{ return w1.getPosition().distanceA(w2.getPosition()); }
	
	public void ajouterLienUnidirectionnel(int waypointOrigine, int waypointDestination)
	{
		this.waypoints[waypointOrigine].ajouterVoisin(this.waypoints[waypointDestination]);
	}
	
	public void ajouterLienBidirectionnel(int w1, int w2)
	{
		this.ajouterLienUnidirectionnel(w1, w2);
		this.ajouterLienUnidirectionnel(w2, w1);
	}
	
	public double getCout(int waypointOrigine, int waypointDestination)
	{ return this.couts[waypointOrigine][waypointDestination]; }
	
	public WayPoint getWayPoint(int numeroWayPoint)
	{ return this.waypoints[numeroWayPoint]; }
	
	public WayPoint getWayPointProche(Coordonnees2D position)
	{
		if(this.nombreWayPoints==0) return null;
		
		WayPoint wayPointProche = this.waypoints[0];
		double distanceMinimum = wayPointProche.getPosition().distanceA(position);
		double distanceCourante;
		
		for(int i=1; i<this.nombreWayPoints; ++i)
		{
			if((distanceCourante=this.waypoints[i].getPosition().distanceA(position))<distanceMinimum)
			{
				wayPointProche = this.waypoints[i];
				distanceMinimum = distanceCourante;
			}
		}
		
		return wayPointProche;
	}
	
	public int getNombreWaypointsMaximum() 
	{ return nombreWaypointsMaximum; }

	public int getNombreWayPoints() 
	{ return nombreWayPoints; }
}